<?php

namespace app\sms\service;

use app\admin\service\BaseService;
use app\sms\model\SmsLogModel;
use app\sms\model\SmsPlatformModel;
use app\sms\model\SmsTemplateModel;
use Exception;

class SmsService extends BaseService{

    protected $platform;
    protected $config;

    const ALIYUN = 'aliyun';//阿里云
    const TENCENT = 'tencentcloud';//腾讯云

    public function __construct($config = [])
    {
        if($config) {
            $this->platform = $config['platform'];
        }else{
            $this->platform = [
                0 => self::ALIYUN
            ];
        }
        $this->config = $config['config'];
    }

    /**
     * 发送短信
     *
     * @Author tanruizheng
     * @DateTime 2021-08-26
     * @param [type] $phone
     * @param array $content
     * @param integer $areaCode
     * @return void
     */
    public function sendSms($phone, $areaCode = 86){
        if(empty($phone)) return self::createFail('手机号不能为空');
        if(empty($areaCode)) return self::createFail('区号不能为空');

        //顺序发送，第一个平台发送失败则执行第二个平台发送
        $count = count(($this->platform));
        foreach ($this->platform as $key => $platform) {
            $file = __DIR__ . DIRECTORY_SEPARATOR . 'platform' . DIRECTORY_SEPARATOR . $platform . DIRECTORY_SEPARATOR . 'Helper.php';
            if(file_exists($file)){
                $className = "\\app\\sms\\service\\platform\\{$platform}\\Helper";
                try {
                    $config = $this->config[$platform];
                    $result = (new $className($config))->sendSms($phone, $areaCode);
                    if($result['status']){
                        return self::createSuccess('发送成功');
                    }else{
                        if($count == $key+1){
                            return self::createFail($result['msg']);
                        } 
                    }
                } catch (\Exception $e) {
                    if($count == $key+1){
                        return self::createFail($e->getMessage());
                    }
                }
            }else{
                return self::createFail('发送失败');
            }
        }
    }

     /**
     * 获取短信平台列表
     *
     * @Author tanruizheng
     * @DateTime 2021-07-12
     * @param array $where
     * @return void
     */
    public static function getPlatformList($params = [], $page = 1, $limit = 15){
        $where = [];
        //短信平台列表
        $list = SmsPlatformModel::where($where)->page($page,$limit)->select()->toArray();
        foreach ($list as &$value) {

        }
        //短信平台总数
        $count = SmsPlatformModel::where($where)->count();
        return self::createList($list,$count);
    }

    /**
     * 获取模板列表
     *
     * @Author tanruizheng
     * @DateTime 2021-07-12
     * @param array $where
     * @return void
     */
    public static function getTemplateList($params = [], $page = 1, $limit = 15){
        $where = [];
        //短信平台列表
        $list = SmsTemplateModel::where($where)->page($page,$limit)->select()->toArray();
        foreach ($list as &$value) {

        }
        //短信平台总数
        $count = SmsTemplateModel::where($where)->count();
        return self::createList($list,$count);
    }

    /**
     * 添加或者编辑模板
     *
     * @Author tanruizheng
     * @DateTime 2021-07-09
     * @param [type] $params
     * @return void
     */
    public static function addOrEditTemplate($params){
        try {
            if(empty($params['template'])){
                return self::createFail('template不能为空');
            }
            if(isset($params['id']) && $params['id']){
                $res = SmsTemplateModel::where('id',$params['id'])->save($params);
            }else{
                $res = SmsTemplateModel::create($params);
            }
            if($res !== false){
                return self::createSuccess('保存成功');
            }
            return self::createFail('保存失败');
        } catch (Exception $e) {
            //输出错误信息
            return self::createFail($e->getMessage());
        }
    }

    /**
     * 模板删除
     *
     * @Author tanruizheng
     * @DateTime 2021-07-13
     * @param [type] $adminId
     * @return void
     */
    public static function templateDelete($id){
        if(!$id){
            return self::createFail('参数错误!');
        }
        $detail = SmsTemplateModel::where('id',$id)->findOrEmpty();
        if(!$detail){
            return self::createFail('信息不存在!');
        }
        $result = $detail->delete();
        if($result !== false){
            return self::createSuccess('删除成功');
        }
        return self::createSuccess('删除失败');
    }

    /**
     * 获取日志列表
     *
     * @Author tanruizheng
     * @DateTime 2021-07-12
     * @param array $where
     * @return void
     */
    public static function getLogList($params = [], $page = 1, $limit = 15){
        $where = [];
        //列表
        $list = SmsLogModel::where($where)->page($page,$limit)->select()->toArray();
        foreach ($list as &$value) {
            $value['sendtime'] = date('Y-m-d H:i:s', $value['sendtime']);
        }
        //总数
        $count = SmsLogModel::where($where)->count();
        return self::createList($list,$count);
    }
}